﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;

namespace UdrinkWater_Project.Admin
{
    public partial class DataEmployee : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       protected void GV_ShowdataEmp_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
       {
           if (e.CommandArgs.CommandName=="Cmd_Edit")
           {
               Session["ID"] = e.CommandArgs.CommandArgument.ToString();
               tbprefix.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Prefix").ToString();
               tbname.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Firstname").ToString();
               tblname.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Lastname").ToString();
               comb_department.Value = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Department_ID");
               tbaddress.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Address").ToString();
               comb_district.Value = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "DISTRICT_ID");
               comb_aumphur.Value = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "AMPHUR_ID");
               comb_province.Value = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "PROVINCE_ID");
               tbtel.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Tel").ToString();
               tbsalary.Text = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Salary").ToString();
               //DateEdit.Value = GV_ShowdataEmp.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "StartJob").ToString();
               pcLogin.ShowOnPageLoad = true;
           }
           if (e.CommandArgs.CommandName=="Cmd_Delete")
           {
               string ID = e.CommandArgs.CommandArgument.ToString();
               Delete_Employee(ID);
               GV_ShowdataEmp.DataBind();
           }
       }

       protected void btOK_Click(object sender, EventArgs e)
       {
           Update_DataEmp();
           GV_ShowdataEmp.DataBind();
       }
       public static object GetDataValue(object value)
       {
           if (value == null)
           {
               return DBNull.Value;
           }

           return value;
       }
       public void Update_DataEmp()
       {
           string sqltext = @"UPDATE [TB_Employee] SET 
                                [Prefix]=@Pre
                               ,[Firstname]=@First
                                ,[Lastname]=@Last
                                ,[Department_ID]=@Depart
                                ,[Address]=@add
                                ,[DISTRICT_ID]=@District
                                ,[AMPHUR_ID]=@Aumphur
                                ,[PROVINCE_ID]=@Province
                                ,[Tel]=@Tel
                                ,[Salary]=@Sal                                
                               WHERE [ID_Card] =@ID ";
           SqlCommand sqlcomm = new SqlCommand();
           SqlConnection sqlconn = new SqlConnection(Connection);
           sqlcomm.Connection = sqlconn;
           sqlcomm.CommandType = System.Data.CommandType.Text;
           sqlcomm.CommandText = sqltext;
           sqlcomm.Parameters.AddWithValue("@ID", GetDataValue(Session["ID"].ToString()));
           sqlcomm.Parameters.AddWithValue("@Pre", GetDataValue(tbprefix.Text));
           sqlcomm.Parameters.AddWithValue("@First", GetDataValue(tbname.Text));
           sqlcomm.Parameters.AddWithValue("@Last", GetDataValue(tblname.Text));
           sqlcomm.Parameters.AddWithValue("@Depart", GetDataValue(comb_department.Value));
           sqlcomm.Parameters.AddWithValue("@add", GetDataValue(tbaddress.Text));
           sqlcomm.Parameters.AddWithValue("@District", GetDataValue(comb_district.Value));
           sqlcomm.Parameters.AddWithValue("@Aumphur", GetDataValue(comb_aumphur.Value));
           sqlcomm.Parameters.AddWithValue("@Province", GetDataValue(comb_province.Value));
           sqlcomm.Parameters.AddWithValue("@Tel", GetDataValue(tbtel.Text));
           sqlcomm.Parameters.AddWithValue("@Sal", GetDataValue(tbsalary.Text));
           //sqlcomm.Parameters.AddWithValue("@Start", GetDataValue(DateEdit.Value));
           try
           {
               sqlcomm.Connection.Open();
               sqlcomm.ExecuteNonQuery();
           }
           catch (Exception)
           {

               sqlcomm.Connection.Close();
               sqlcomm.Connection.Dispose();
           }
           sqlcomm.Connection.Close();
           sqlcomm.Connection.Dispose();

       }
        
       public void Delete_Employee(string ID)
       {
           string sqltext = @"DELETE FROM [TB_Employee] WHERE ID_Card='" + ID + "'";
           SqlCommand sqlcomm = new SqlCommand();
           SqlConnection sqlconn = new SqlConnection(Connection);
           sqlcomm.Connection = sqlconn;
           sqlcomm.CommandType = System.Data.CommandType.Text;
           sqlcomm.CommandText = sqltext;
           try
           {
               sqlcomm.Connection.Open();
               sqlcomm.ExecuteNonQuery();
           }
           catch (Exception)
           {

               sqlcomm.Connection.Close();
               sqlcomm.Connection.Dispose();
           }
           sqlcomm.Connection.Close();
           sqlcomm.Connection.Dispose();

       }

       protected void bt_AddEmp_Click(object sender, EventArgs e)
       {
           Response.Redirect("Register.aspx");
       }
       

      

       
       
}
    
    }
